﻿using Code;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace github.heerheer.card2.Code.Helper
{
    public class CommandHelper
    {
        public CommandHelper()
        {
            Initalize();
        }

        private void Initalize()
        {
            setPath(path);
        }

        private string path = AppMain.Common.AppDirectory + "Commands.txt";

        public void setPath(string path)
        {
            if (!File.Exists(path))
            {
                File.Create(path).Close();
                File.WriteAllText(path,
                    new StreamReader(
                        Assembly.GetExecutingAssembly().GetManifestResourceStream(
                            //github.heerheer.card2.Code.Langs.zh_cn.lang
                            "github.heerheer.card2.Code.Helper.Langs.Commands.txt")
                        ).ReadToEnd()
                );
            }
            else
            {
                var sr =
                    new StreamReader(
                        Assembly.GetExecutingAssembly().GetManifestResourceStream(
                            //github.heerheer.card2.Code.Langs.zh_cn.lang
                            "github.heerheer.card2.Code.Helper.Langs.Commands.txt")
                        );

                var mylines = File.ReadAllLines(path);

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var pair = getKeyValue(line);
                    if (pair.Key != "")
                    {
                        if (mylines.ToList<string>().Find(s => s.StartsWith(pair.Key + "=")) == null)
                        {
                            File.AppendAllLines(path, new List<string>() { line });
                        }
                    }
                }
                sr.Close();
            }
        }

        public string CheckinCommand
        {
            get
            {
                return getCommandStr("Command.Checkin");
            }
        }

        public string BagCommand
        {
            get
            {
                return getCommandStr("Command.Bag");
            }
        }
        public string PoolInfoCommand
        {
            get
            {
                return getCommandStr("Command.PoolInfo");
            }
        }

        public string TestMeCommand
        {
            get
            {
                return getCommandStr("Command.TestLucky");
            }
        }


        public string EcoCommand
        {
            get
            {
                return getCommandStr("Command.Eco");
            }
        }
        public string EcoCommandReply
        {
            get
            {
                return getCommandStr("Reply.Eco.GiveSuccess");
            }
        }

        private string getCommandStr(string ckey)
        {
            var lines = File.ReadAllLines(path);
            foreach (var line in lines)
            {
                if (line.Trim() == "")
                    continue;
                if (!line.StartsWith("#"))
                {
                    var pair = getKeyValue(line);
                    if (pair.Key == ckey)
                    {
                        if (pair.Value == "")
                        {
                            return ckey;
                        }
                        return pair.Value;
                    }
                }
            }
            return ckey;
        }

        /// <summary>
        /// 获得指定的指令自定义语句
        /// </summary>
        /// <param name="ckey"></param>
        /// <returns></returns>
        public string getCommand(string ckey)
        {
            return getCommandStr(ckey);
        }

        private KeyValuePair<string, string> getKeyValue(string line)
        {
            if (!line.StartsWith("#") && line.Contains("="))
            {
                var key = "";
                var value = "";
                key = line.Split('=')[0];
                value = line.Split('=')[1];
                return new KeyValuePair<string, string>(key, value);
            }
            else
            {
                return new KeyValuePair<string, string>("", "");
            }
        }
    }
}